define(['angularAMD'], function (angularAMD) {
    angularAMD.service('E04Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog',
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog){
            var _service = this;
            // 生產線開窗
            _service.loadLine = function(data, callback){
                $MMWService.showQueryListPNIG({
                    page_mode: 26,
                    header: [
                        {label:$filter('translate')('common.queryList.area_c'), code:'page_no'},
                        {label:$filter('translate')('common.queryList.area_n'), code:'page_name'}
                    ],
                    setCondition:function(conditions){
                        return conditions;
                    },
                    selectCode: data.pdline_no,
                    confirm : function(selectItem){
                        data.pdline_no = selectItem.page_no;
                        data.pdline_name = selectItem.page_name;
                        data.op_no = '';
                        data.op_name = '';
                        data.position_no = '';
                        if(typeof(callback) == 'function')
                            callback(data);
                    }
                });
            };
            // 作業站開窗
            _service.loadOP = function(data, callback){
                var msg = '';
                if(data.pdline_no == ''){
                    msg += $filter('translate')('E01.msg.line_no_empty');
                }
                if(msg == ''){
                    $MMWService.showQueryListPNIG({
                        page_mode: 27,
                        header: [
                            {label:$filter('translate')('common.queryList.op_c'), code:'page_no'},
                            {label:$filter('translate')('common.queryList.op_n'), code:'page_name'}
                        ],
                        setCondition:function(conditions){
                            conditions.push({column_name:'PDLineNo', query_mode:'0', merge_condition_model:'AND', value:data.pdline_no });
                            return conditions;
                        },
                        selectCode:data.op_no,
                        confirm : function(selectItem){
                            data.op_no = selectItem.page_no;
                            data.op_name = selectItem.page_name;
                            data.position_no = '';
                            if(typeof(callback) == 'function')
                                callback(data);
                        }
                    });
                } else {
                    $rootScope.showAlert(msg);
                }
            }
            //工位開窗
            _service.loadPosition = function(data, callabck){
                var msg = '';
                if(data.pdline_no == ''){
                    msg += $filter('translate')('E01.msg.line_no_empty')+'<br>';
                }
                if(data.op_no == ''){
                    msg += $filter('translate')('E01.msg.op_no_empty')+'<br>';
                }
                if(msg == ''){
                    $MMWService.showSelectPNIG({
                        page_mode : '28',
                        query_condition_info:[
                            {column_name:'PDLineNo', query_mode:'0', merge_condition_model:'AND', value:data.pdline_no },
                            {column_name:'OPNo', query_mode:'0', merge_condition_model:'AND', value:data.op_no }
                        ]
                    },
                    $filter('translate')('common.msg.plz_select'),
                    data.position_no,
                    function(item, selectDialog){
                         data.position_no = item.page_no;
                         if(typeof(callback) == 'function')
                             callback(data);
                    });
                } else {
                    $rootScope.showAlert(msg);
                }
            }
            // 工位機物料設定
            _service.gotoE05 = function(data){
                $rootScope.hideMenu();
                $rootScope.changeProgram('E05', {data:{
                    pdline_no:data.pdline_no,
                    op_no:data.op_no,
                    position_no:data.position_no,
                    single_confirm:data.single_confirm
                }});
            }
            _service.init = function(options){
                $mdDialog.dialog(
                    'module/moduleE/program/E04/E04.html',
                    function(dialog){
                        return {
                            css_url:'module/moduleE/program/E04/E04.css',
                            parent:options.parent,
                            stationMachine:options.stationMachine,
                            beforeShown: function(){
                            },
                            loadLine: function(){
                                _service.loadLine(dialog.stationMachine, function(data){
                                    dialog.stationMachine = data;
                                });
                            },
                            loadOp: function(){
                                _service.loadOP(dialog.stationMachine, function(data){
                                    dialog.stationMachine = data;
                                });
                            },
                            loadPosition: function(){
                                _service.loadPosition(dialog.stationMachine, function(data){
                                    dialog.stationMachine = data;
                                });
                            },
                            gotoE05:function(){
                                dialog.hide();
                                _service.gotoE05(dialog.stationMachine);
                            },
                            back: function () {
                                dialog.hide();
                                if(options.close)
                                    options.close();
                            },
                            confirm: function () {
                                if(options.callback)
                                    options.callback(dialog);
                            }
                        };
                    }
                );
            };
        }]);
});
